package com.epta.service;

import com.epta.pojo.Expert;
import com.epta.common.util.DateTimeUtil;
import com.epta.vo.ExpertBackListVo;
import com.epta.vo.ExpertFrontListVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/20 0020.
 */
@Service
public class ExpertService extends BaseService<Expert>{
    @Autowired
    private FieldService fieldService;

    public PageInfo<ExpertBackListVo> queryBackList(int page,int rows){
        PageInfo pageInfo = this.queryPageListByWhere(null,page,rows);
        List<ExpertBackListVo> list = new ArrayList<>();
        for(Object o : pageInfo.getList()){
            Expert expert = (Expert) o;
            ExpertBackListVo expertBackListVo = assmbleExpertBackListVo(expert);
            list.add(expertBackListVo);
        }
        pageInfo.setList(list);
        return pageInfo;
    }

    public PageInfo<ExpertFrontListVo> queryFrontList(int page,int rows){
        PageInfo pageInfo = this.queryPageListByWhere(null,page,rows);
        List<ExpertFrontListVo> list = new ArrayList<>();
        for(Object o : pageInfo.getList()){
            Expert expert = (Expert) o;
            ExpertFrontListVo expertFrontListVo = assmbleExpertFrontListVo(expert);
            list.add(expertFrontListVo);
        }
        pageInfo.setList(list);
        return pageInfo;
    }

    private ExpertFrontListVo assmbleExpertFrontListVo(Expert expert){
        ExpertFrontListVo expertFrontListVo = new ExpertFrontListVo();
        expertFrontListVo.setId(expert.getId());
        expertFrontListVo.setName(expert.getName());
        return expertFrontListVo;
    }
    
    private ExpertBackListVo assmbleExpertBackListVo(Expert expert){
        ExpertBackListVo expertBackListVo = new ExpertBackListVo();
        expertBackListVo.setId(expert.getId());
        expertBackListVo.setTime(DateTimeUtil.dateToStr(expert.getCreated()));
        expertBackListVo.setName(expert.getName());
        String sex = expert.getSex() == 0 ? "男":"女";
        expertBackListVo.setSex(sex);
        expertBackListVo.setField(fieldService.queryById(expert.getFieldId()).getField());
        return expertBackListVo;
    }
}
